<?php

namespace app\Admin\controller;

use think\Controller;
use think\Request;

class User extends Controller
{
    public function login(){
       return view();
    }

    public function tologin(Request $request){
        $params = input();
        $params['username'] = $params['phone'];
        $validate = $this->validate($params,[
            'phone'=>'reuqire|regex:1[3689]\d{9}',
            'password'=>'reuqire|confirm:repassword',
            'code'=>'reuqire'
        ]);
        if ($validate !=true){
            return json(['code'=>200,'msg'=>'参数错误']);
        }
        $code = cache('code');
         if ($code != $params['code']){
             return json(['code'=>200,'msg'=>'验证码不正确']);
         }
         \app\Admin\model\User::create($params,true);
        return json(['code'=>300,'msg'=>'注册成功']);
    }

    public function dologin(Request  $request){
        $params = input();
        $data = \app\Admin\model\User::where(function($query)use ($params){
            $query->where('username', $params['username'])
                ->whereOr('phone',  $params['username'])
                ->whereOr('email', $params['username']);
        })->find();
        $info= $data->toArray();
        if ($info['password'] == $params['password']){
            return json(['code'=>300,'msg'=>'登录成功']);
        }else{
            return json(['code'=>200,'msg'=>'登陆失败']);
        }
    }



   public function send(){
        $params = input();
        $phone = $params['phone'];
        $code= rand(1000,9999);
       $flag = sendcode($phone,[$code,2]);
       if ($flag){
           cache("code",$code);
           return json(['code'=>300,'data'=>$code,'msg'=>"发送成功"]);
       }else{
           return json(['code'=>200,'data'=>'','msg'=>"发送失败"]);
       }
   }

   public function register(){
       return view();
   }
}
